﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft;
using Newtonsoft.Json;
namespace JsonConversion
{
    /// <summary>
    /// This class is used in creation of JSON Object.
    /// </summary>
    public class JsonObject
    {
        public Dictionary<string, object> oList = new Dictionary<string, object>();

        /// <summary>
        /// creates JSON Object.
        /// </summary>
        /// <returns>string containing JSON Object</returns>
        public string GetJsonString()
        {

            string Json = string.Empty;
            int idx = 0;
            JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
            foreach (string Key in oList.Keys)
            {
                Json += (idx == 0 ? "" : ",") + '"' + Key + '"' + ":" + Newtonsoft.Json.JsonConvert.SerializeObject(oList[Key], microsoftDateFormatSettings);
                idx++;
            }
            return "{" + Json + "}";
        }

        /* 
         *  How to use JsonObject Class
            JsonObject oJson = new JsonObject();
            oJson.oList.Add("oUser", new CUser());
            oJson.oList.Add("oAuth", new CAuthentication());
            oJson.oList.Add("oUser1", new CUser());
            oJson.oList.Add("oUser3", new CUser());
            oJson.oList.Add("BussinessUnitId","1");
            textBox1.Text = oJson.GetJsonString();
         */
    }
}
